//index.js
//获取应用实例
var app = getApp()
Page({
    data: {
        scrollTop: {
            scroll_top: 0,
            goTop_show: false
        },
        isshow_subsidy: true,
        showMotal:false,
        showMotalTxt:"为您找到一个商品",
        showMotalUrl:"",
        showMotalButton:false,
        isshow_subsidy: true,
        scrollHeight: 0,
        pageheight: 1200,
        indicatorDots: true,
        autoplay: false,
        interval: 3000,
        duration: 100,
        page: 1,
        sort: 0,
        cname: "",
        cname_show: "",
        keyword: "",
        pagesize: 20,
        uri: "list",
        from_uid: 0,
        memberid: 0,
        globalset: {},
        datalock: false,
        commission: 0,
      
        commodity: [],
        tiptext:"请稍后，一大波数据正在赶来……"
    },

    onLoad: function (options) {
    
        var $this = this;
        wx.getStorage({
            key: 'searchkey',
            success: function (res) {
             
                $this.setData({
                    searchkey: res.data
                })
            }
        })
        
        
        wx.getSystemInfo({
            success: function (res) {
                $this.setData({
                    scrollHeight: res.windowHeight
                });
            }
        });
    },
    onReady: function () {
      
        var $this = this;

        var cname = wx.getStorageSync("cname");
        if (cname != "") {
            $this.setData({
                cname: cname,
                uri: "listbycname"
            });
        }
        var cname_show = wx.getStorageSync("cname_show");
        if (cname_show != "") {
            $this.setData({
                cname_show: cname_show
            });
        }
        var searchkey = wx.getStorageSync("searchkey");
        if (searchkey != "") {
            $this.setData({
                keyword: searchkey,
                cname: "",
                uri: "listbysearch"
            });
        }


        //取会员对象的方式
        var memberinfo = wx.getStorageSync('memberinfo');

        $this.setData({
            memberinfo:memberinfo,
            from_uid: memberinfo.memberid,
            memberid: memberinfo.memberid,
        });
    
        var globalset = wx.getStorageSync('globalset');
        //取会员对象的方式
        $this.setData({
            globalset: globalset
        });
        //图标菜单
        var head_menu_str = globalset.head_menu;
        var head_menu_json = JSON.parse(head_menu_str);
        var menulen = head_menu_json.list.length;
        $this.setData({
            imgmenu: head_menu_json.list,
            datalock: false
        });
        var imgmenu2 = new Array();
        for (var i = 8; i < menulen; i++) {
            imgmenu2.push(head_menu_json.list[i]);
        }
        $this.setData({
            imgmenu2: imgmenu2,
            datalock: false
        });
        //计算总的佣金比例
        for (var i = 0; i < globalset.memberlevel.length; i++) {
          
            //当前会员类型的等级佣金比例
            if (globalset.memberlevel[i].type == memberinfo.type && globalset.memberlevel[i].name == memberinfo.level) {
                var total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2) + parseFloat(globalset.memberlevel[i].myleader1_credit2) + parseFloat(globalset.memberlevel[i].myleader2_credit2);
                //换成只取1级佣金的比例
                var total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2);
              
                $this.setData({
                    commission: total_commission,
                    datalock: false
                });
             
            }
        }
        if (memberinfo.type == 0) {
          if (globalset.isopen_subsidy == 0) {

            $this.setData({
              isshow_subsidy: false
            });
          }
         
          var total_commission = parseFloat(globalset.subsidy);
          $this.setData({
            commission: total_commission,
            datalock: false
          });
        }
        if (globalset.isshow_subsidy == 0) {
            $this.setData({
                isshow_subsidy: false
            });
        }
    
        var limittime = wx.getStorageSync('limittime');
        var realtime = wx.getStorageSync('realtime');
        if (!isNaN($this.data.keyword) && $this.data.keyword!="") {
          //是数字查询skuid
        
          $this.getUnionurlbysku();
          
        } else {
          $this.goodList(1);
        }

    },

    onReachBottom: function () {
     
        var $this = this;
        var page = $this.data.page + 1;
        var second_height = $this.data.second_height * page;
        $this.setData({ second_height: second_height });
        $this.goodList(page);
    },
    //事件处理函数
    skiptodetail: function (e) {
        var skuId = e.currentTarget.dataset.skuid;
        for (var i = 0; i < this.data.commodity.length; i++) {
            if (this.data.commodity[i].skuId == skuId) {
                var good = this.data.commodity[i];
                wx.setStorage({
                    key: "good",
                    data: good
                });
            }
        }

        wx.navigateTo({
            url: '../detail/index?skuId=' + skuId
        })
    },
    bindChange: function (e) {

        var that = this;
        that.setData({ currentTab: e.detail.current });

    },

    swichNav: function (e) {

        var that = this;

        if (this.data.currentTab === e.target.dataset.current) {
            return false;
        } else {
            that.setData({
                currentTab: e.target.dataset.current
            })
        }
    },
    goodList: function (page, callback) {
        var $this = this;
        if ($this.data.datalock) {
            return false;
        }
        $this.setData({
            page: page,
            datalock: true,
        });
     
        var limittime = wx.getStorageSync('limittime');
        var realtime = wx.getStorageSync('realtime');
        if ($this.data.keyword!=""){
          limittime="";
          realtime="";
        }
        app.util.request({
            url: 'entry/wxapp/Goods',
            data: {
                m: 'nets_haojk',
                page: page,
                uri: $this.data.uri,
                sort: $this.data.sort,
                keyword: $this.data.keyword,
                cname: $this.data.cname,
                limittime: limittime,
                realtime: realtime
            },
            method: 'post',
            success: function (response) {
             
                if (typeof callback == 'function') {
                    callback(response.data.data);
                    $this.setData({
                        datalock: false
                    });
                } else {
                    var goods = $this.data.commodity;
                    if (response.data.data) {
                        for (var i in response.data.data) {
                            if ($this.data.commission == 0) {
                                response.data.data[i].wlPrice_after1 = 0.00;
                            } else {
                                response.data.data[i].wlPrice_after1 = response.data.data[i].wlPrice_after * (response.data.data[i].wlCommissionShare / 100) * ($this.data.commission / 100)*0.9;
                                if (response.data.data[i].wlPrice_after1 > 0)
                                    response.data.data[i].wlPrice_after1 = response.data.data[i].wlPrice_after1.toFixed(2);
                               


                            }
                            goods.push(response.data.data[i]);
                        }
                    }else{
                        $this.setData({"tiptext":"没有更多了，换个词试试"});
                    }
                    $this.setData({
                        commodity: goods,
                        datalock: false
                    });
                }
            }
        });
    },
    getUnionurlbysku: function (page, callback) {
    var $this = this;
    
    $this.setData({
      page: page,
      datalock: true,
    });
   
    app.util.request({
      url: 'entry/wxapp/Unionurlbysku',
      data: {
        m: 'nets_haojk',
        keyword: $this.data.keyword
      },
      method: 'post',
      success: function (response) {
    
        var goods = $this.data.commodity;
        if (response.data.data && response.data.data.info!="") {
          var goodstemp=Array();
          goodstemp.push(response.data.data.info);
         
          for (var i in goodstemp) {
            if ($this.data.commission == 0) {
              goodstemp.wlPrice_after1 = 0.00;
            } else {
              goodstemp[i].wlPrice_after1 = goodstemp[i].wlPrice_after * (goodstemp[i].wlCommissionShare / 100) * ($this.data.commission / 100)*0.9;
              if (goodstemp[i].wlPrice_after1 > 0)
                goodstemp[i].wlPrice_after1 = goodstemp[i].wlPrice_after1.toFixed(2);
              


            }
            goods.push(goodstemp[i]);
          }
          $this.setData({
            commodity: goods,
            datalock: false
          });
          return;
        }
        
        if (response.data.data.data!=""){
       
          $this.setData({
            showMotal: true,
            showMotalTxt: "为您找到一个商品",
            showMotalButton: true,
            showMotalUrl: response.data.data.data
          });
        }else{
          $this.setData({
            showMotal: true,
            showMotalTxt:"没有找到相关商品",
            showMotalButton: false,
          });
        }
        
        
        
      }
    });
  },
    bysort: function (e) {
        var $this = this;

        wx.getSystemInfo({
            success: function (res) {
                $this.setData({
                    scrollHeight: res.windowHeight
                });
            }
        });
        this.setData({
            sort: e.currentTarget.dataset.sort
        });
        this.setData({
            commodity: [],
            datalock: false
        });
        this.goodList(1);
    },
    bycname: function (e) {
        var $this = this;

        wx.getSystemInfo({
            success: function (res) {
                $this.setData({
                    scrollHeight: res.windowHeight
                });
            }
        });
     
        var cname = e.currentTarget.dataset.url.split("=")[1];
        this.setData({
            cname_show: e.currentTarget.dataset.cname
        });

        this.setData({
            cname: cname,
            uri: "listbycname"
        });
        this.setData({
            commodity: [],
            datalock: false
        });
        this.setData({
            keyword: ""
        });
        wx.setStorage({
            key: "cname",
            data: e.currentTarget.dataset.cname
        });
        wx.setStorage({
            key: "searchkey",
            data: ""
        });
        this.goodList(1);
    },
    searchkey: function (e) {
        this.setData({
            commodity: [],
            datalock: false
        });

   
        if (!isNaN(this.data.keyword)) {
          //是数字查询skuid
       
          this.getUnionurlbysku();
          return false;
        }else{ 
          this.goodList(1);
        }
    },
    bindKeyInput: function (e) {
      var value = e.detail.value;
      var Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
      var objExp = new RegExp(Expression);
      if (objExp.test(value) == true) {
        //是URL
      
        if (value.indexOf(".html") > 0) {
          value = value.split('?')[0];
          var valuearr = value.split("/");
          value = valuearr[valuearr.length - 1];
          value = value.replace(".html", "");
        }
        
        if (value.indexOf("sku=") > 0) {
          value = value.split('sku=')[1];
     
          var valuearr = value.split("&");
          value = valuearr[0];
      
        }
      }
        this.setData({
          keyword: value,
            cname: "",
            uri: "listbysearch"
        });

        wx.setStorage({
            key: "searchkey",
            data: value
        });
        wx.setStorage({
            key: "cname",
            data: ""
        });
    },
    onShareAppMessage: function (res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
        }
        return {
            title: this.data.globalset.title,
            path: '/haojk/pages/index/index?from_uid=' + this.data.memberid,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    },
    scrollTopFun: function (e) {
     
        this.setData({
            'pageheight': e.detail.scrollHeight
        });
        if (e.detail.scrollTop > 300) {//触发gotop的显示条件 
            this.setData({
                'scrollTop.goTop_show': true
            });
        } else {
            this.setData({
                'scrollTop.goTop_show': false
            });
        }
    },
    goTopFun: function (e) {
        var _top = this.data.scrollTop.scroll_top;//发现设置scroll-top值不能和上一次的值一样，否则无效，所以这里加了个判断 
        if (_top == 0) {
            _top = 1;
        } else {
            _top = 0;
        }
        this.setData({
            'scrollTop.scroll_top': _top
        });
    },
    goBack:function(){
      
        wx.navigateBack({
          delta: 1
        });
       
        
    },
    closeModal: function () {
        var $that = this;
    
        if ($that.data.showMotal) {
          
            $that.setData({
                showMotal: false
            })
          

        } else {
            
            $that.setData({
                showMotal: true
            })
        
        }


    },
})
